package com.xiaoge.biz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoge.auth.annotation.ControllerEndpoint;
import com.xiaoge.biz.entity.InStock;
import com.xiaoge.biz.entity.Product;
import com.xiaoge.biz.entity.Supplier;
import com.xiaoge.biz.service.InStockService;
import com.xiaoge.biz.service.ProductService;
import com.xiaoge.biz.service.SupplierService;
import com.xiaoge.biz.vo.InStockVo;
import com.xiaoge.biz.vo.IntoStockVo;
import com.xiaoge.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xiaoge
 * @since 2021-03-21
 */
@CrossOrigin
@RestController
@RequestMapping("/inStock")
public class InStockController {

    @Autowired
    private InStockService inStockService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private ProductService productService;

    /**
     * 分页查询入库列表
     *
     * @param current
     * @param size
     * @param inStockVo
     * @return
     */
    @ApiOperation(value = "分页查询入库列表")
    @PostMapping("/findInStockList")
    public Result findInStockList(@RequestParam(defaultValue = "1") Integer current,
                                  @RequestParam(defaultValue = "6") Integer size,
                                  @RequestBody InStockVo inStockVo) {
        //取得分页条件
        Page<InStock> page = new Page<>(current, size);
        //获得wragger
        QueryWrapper<InStock> queryWrapper = getWrapper(inStockVo);

        //执行查询
        IPage<InStock> productPage = inStockService.findInStockList(page, queryWrapper);

        long total = productPage.getTotal();
        List<InStock> records = productPage.getRecords();
        return Result.ok().data("total", total).data("records", records);
    }

    /**
     * 入库申请
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "入库单申请失败", operation = "入库申请")
    @ApiOperation(value = "入库申请")
    @PostMapping("/addIntoStock")
    public Result addIntoStock(@RequestBody IntoStockVo intoStockVo) {
        //新增来源
        if (intoStockVo.getSupplierId() == null) {
            Supplier supplier = new Supplier();
            supplier.setAddress(intoStockVo.getAddress());
            supplier.setContact(intoStockVo.getContact());
            supplier.setEmail(intoStockVo.getEmail());
            supplier.setName(intoStockVo.getName());
            supplier.setPhone(intoStockVo.getPhone());
            supplier.setSort(intoStockVo.getSort());
            Supplier supplier1 = supplierService.add(supplier);
            intoStockVo.setSupplierId(supplier1.getId());
        }

        inStockService.addIntoStock(intoStockVo);
        return Result.ok();
    }

    /**
     * 物资入库单详细
     *
     * @param
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "查询物资入库单详细失败", operation = "物资入库单详细")
    @ApiOperation(value = "物资入库单详细")
    @PostMapping("/detail")
    public Result detail(@RequestParam String inNum,
                         @RequestParam Long supplierId,
                         @RequestParam(defaultValue = "1") Integer current,
                         @RequestParam(defaultValue = "3") Integer size) {
        //取得分页条件
        Page<Product> page = new Page<>(current, size);

        //执行查询
        IPage<Product> productPage = productService.getDetails(page, inNum);

        Supplier supplier = supplierService.getById(supplierId);

        long total = productPage.getTotal();
        List<Product> records = productPage.getRecords();
        return Result.ok().data("total", total).data("records", records).data("supplier", supplier);
    }

    /**
     * 恢复入库清单
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "恢复入库清单失败", operation = "恢复入库清单")
    @ApiOperation(value = "恢复入库清单")
    @PostMapping("/backInStock")
    public Result backInStock(Long id) {
        InStock inStock = new InStock();
        inStock.setId(id);
        inStock.setStatus(0);
        inStockService.updateById(inStock);
        return Result.ok();
    }

    /**
     * 移至回收站
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "移至回收站失败", operation = "移至回收站")
    @ApiOperation(value = "移至回收站")
    @PostMapping("/removeInStock")
    public Result removeInStock(Long id) {
        InStock inStock = new InStock();
        inStock.setId(id);
        inStock.setStatus(1);
        inStockService.updateById(inStock);
        return Result.ok();
    }

    /**
     * 审核通过
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "审核失败", operation = "审核通过")
    @ApiOperation(value = "审核通过")
    @PostMapping("/publishInStock")
    public Result publishInStock(Long id,String inNum) {
        inStockService.publishInStock( id, inNum);
        return Result.ok();
    }

    /**
     * 删除入库清单
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除入库清单失败", operation = "删除入库清单")
    @ApiOperation(value = "删除入库清单")
    @DeleteMapping("/delInStock")
    public Result delInStock(Long id,String inNum) {
        inStockService.delete(id,inNum);
        return Result.ok();
    }

    private QueryWrapper<InStock> getWrapper(InStockVo inStockVo) {
        QueryWrapper<InStock> queryWrapper = new QueryWrapper<>();
        if (inStockVo != null) {

            //查询每个条件
            if (!ObjectUtils.isEmpty(inStockVo.getStatus())) {
                queryWrapper.eq("status", inStockVo.getStatus());
            }
            if (!ObjectUtils.isEmpty(inStockVo.getInNum())) {
                queryWrapper.like("in_num", inStockVo.getInNum());
            }
            if (!ObjectUtils.isEmpty(inStockVo.getType())) {
                queryWrapper.eq("type", inStockVo.getType());
            }
            if (!ObjectUtils.isEmpty(inStockVo.getStartTime())) {
                queryWrapper.ge("i.create_time", inStockVo.getStartTime());
            }
            if (!ObjectUtils.isEmpty(inStockVo.getEndTime())) {
                queryWrapper.le("i.create_time", inStockVo.getEndTime());
            }
        }
        return queryWrapper;
    }
}
